<?php
      // 接收微信传来的值
      $timestamp = $_GET['timestamp'];
      $nonce      = $_GET['nonce'];
      $token = 'ppv5';
      $signature = $_GET['signature'];
      // 放入数组准备字典序排序
      $tem_arr = array($timestamp,$nonce,$token);
      sort($tem_arr);
      // 将排序好的字符串拼接起来
      $tempStr = implode('',$tem_arr);
      $tempStr = sha1($tempStr);
      if($tempStr==$signature && $_GET['echostr']){
            echo  $_GET['echostr'];
            exit;
      }else{
            // 接收微信发过来的信息 xml
            $postxml = $GLOBALS['HTTP_RAW_POST_DATA'];
            // 将xml 解析成 对象
            $postobj = simplexml_load_string($postxml);
            // 回复准备
            $toUser = $postobj->FromUserName;
            $formUser = $postobj->ToUserName;
            $MsgType = $postobj->MsgType;
            $content = $postobj->Content;

            // 纯文本回复模板
            $tempxml = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
            // 判断是否是事件推送
            if(strtolower($MsgType)=='event'){
                  if(strtolower($postobj->Event) == 'subscribe'){
                        $replyContent = "欢迎关注ppv5公众号";
                  }
            }else {

                  $replyContent = "此公众号还在开发中";
            }
            // 填充模板
            $str = sprintf($tempxml,$toUser,$formUser,time(),"text",$replyContent);

            echo $str;
      }


?>